package com.aios.action.timer;

import java.util.Iterator;
import java.util.List;
import java.util.Locale;
import java.util.Map;
import org.apache.log4j.Logger;
import com.aios.entity.unit.DayName;

public class TimerSaveAction extends AbstractTimerAction {

	private static final long serialVersionUID = 1L;
	private static Logger logger = Logger.getLogger(TimerSaveAction.class);
	private static final int HOUR_VALUE=60;
	private List<Integer> selectedDays;
	private List<String> selected_hour;
	private List<String> selected_min;
	private final static int keynotfound = -1;
	private String timerStatus;

	public List<String> getSelected_hour() {
		return selected_hour;
	}

	public void setSelected_hour(List<String> selected_hour) {
		this.selected_hour = selected_hour;
	}

	public List<String> getSelected_min() {
		return selected_min;
	}

	public void setSelected_min(List<String> selected_min) {
		this.selected_min = selected_min;
	}

	public List<Integer> getSelectedDays() {
		return selectedDays;
	}

	public void setSelectedDays(List<Integer> selectedDays) {
		this.selectedDays = selectedDays;
	}

	public String execute() {

		/*
		 * validateTimer();
		 * 
		 * if(hasErrors()) { return INPUT; }
		 */
		if (getMode() == Mode.ADD) {

			try {
				
				saveAddTimerSettings(timerSetting.getTimer());
				
				setTime();
				timerService.save(timerSetting.getTimer(),getSessionUserVariable().getId(),Byte.valueOf(timerStatus));
				timerService.addNewDays(timerSetting.getTimer().getId(), selectedDays);
				actionMessage = getText("success.insert");
			} catch (Exception e) {
				logger.error(e);
				actionError = getText("error.insert");
				return INPUT;
			}

		} else if (getMode() == Mode.EDIT) {
			try {
				saveEditSettings(timerSetting.getTimer());
				setTime();
				
				timerSetting.getTimer().setStatus(Byte.valueOf(timerStatus));
				timerService.findDaySchedule(timerSetting, selectedDays,
						getSessionUserVariable().getId());
				timerService.update(timerSetting.getTimer());
				actionMessage = getText("success.update");
			} catch (Exception e) {
				logger.error(e);
				actionError = getText("error.update");
				return INPUT;
			}
			// validation
		} else {
			// add action error
			return INPUT;
		}

		return SUCCESS;
	}

	private void validateTimer() {

		if (timer.getName().isEmpty()) {
			addFieldError("timer.name", getText("error.name.empty"));
		}

	}

	private void setTime() {
		String hour = selected_hour.get(0);
		String min = selected_min.get(0);
		int inthour = Integer.parseInt(hour);
		int intmin = Integer.parseInt(min);
		int realtime = inthour * HOUR_VALUE + intmin;
		timerSetting.getTimer().setTrigerTime(realtime);

	}
	
	private int getKeyDayMap(String value) {
		Map<Integer, String> dayMap = DayName.getMap(Locale.JAPAN);

		Iterator<Integer> itr = dayMap.keySet().iterator();
		int key;
		String mapvalue;
		while (itr.hasNext()) {
			key = itr.next();
			mapvalue = dayMap.get(key);
			if (mapvalue.equals(value)) {
				return key;
			}
		}
		return keynotfound;
	}

	public String getTimerStatus() {
		return timerStatus;
	}

	public void setTimerStatus(String timerStatus) {

		if (timerStatus.equals("false")) {
			this.timerStatus = "0";
		} else {
			this.timerStatus = timerStatus;
		}

	}

}
